﻿using System;
using System.Collections.Generic;

namespace TowerDefence_Zombie.Data
{
    public enum CurrencyEvent
    {
        ZombieDrop,          //僵尸掉落 0
        RecycleTower,        //回收炮塔 1
        BuyTower,            //购买炮塔 2
        BuyShopTreasureChest,//购买商城宝箱 3
        BuyDoubleAtkBuff,    //购买攻击Buff 4
        BuyDoubleGoldBuff,   //购买金币Buff 5
        DoubleGoldBuffReturn,//金币Buff返还 6
        DoubleAtkBuffReturn, //攻击Buff返还 7
        LevelTreasureChest,  //关卡宝箱 8
        ShopTreasureChest,   //商城宝箱 9
        Sigin,               //每日签到 10
        Task,                //任务系统 11
        OfflineEarnings,     //离线收益 12
        ItemStarUpgrade,     //物品升星 13
    }

    public sealed partial class DataPlayer
    {
        public int OfflineEarning = 0;

        public bool CheckCurrencyIsMeet(int[] moneyIds, long[] num)
        {
            for (int i = 0; i < moneyIds.Length; i++)
            {
                if (i >= num.Length)
                {
                    break;
                }
                if (CheckCurrencyIsMeet(moneyIds[i], num[i]) == false)
                {
                    return false;
                }
            }
            return true;
        }

        public bool CheckCurrencyIsMeet(int moneyId, long meetNum)
        {
            var dtMoney = GameEntry.Data.GetData<MoneyEntity>();
            if (dtMoney.TryGetMoneyData(moneyId, out var moneyData))
            {
                return GetCurrency(moneyId, moneyData.InitNum) >= meetNum;
            }
            return false;
        }

        public long GetCurrency(CurrencyType moneyType)
        {
            if (GameEntry.Data.GetData<MoneyEntity>().GetCurrencyId(moneyType, out var moneyId)
                && _playerData.MoneyTypeDataDic.TryGetValue(moneyId, out var moneyNum))
            {
                return moneyNum;
            }
            return 0;
        }

        public long GetCurrency(int moneyId)
        {
            if (_playerData.MoneyTypeDataDic.TryGetValue(moneyId, out var moneyNum))
            {
                return moneyNum;
            }
            return 0;
        }

        public long GetCurrency(int moneyId, long initMoneyNum)
        {
            if (!_playerData.MoneyTypeDataDic.TryGetValue(moneyId, out var moneyNum))
            {
                moneyNum = initMoneyNum;
                _playerData.MoneyTypeDataDic.Add(moneyId, moneyNum);
            }
            return moneyNum;
        }

        public void RemoveCurrency(int[] moneyIds, long[] num, CurrencyEvent currencyEvent)
        {
            for (int i = 0; i < moneyIds.Length; i++)
            {
                if (i >= num.Length)
                {
                    break;
                }
                else RemoveCurrency(moneyIds[i], num[i], currencyEvent);
            }
        }

        public void RemoveCurrency(int moneyId, long num, CurrencyEvent currencyEvent)
        {
            var dtMoney = GameEntry.Data.GetData<MoneyEntity>();
            if (dtMoney.TryGetMoneyData(moneyId, out var moneyData))
            {
                var moneyNum = GetCurrency(moneyId, moneyData.InitNum);
                moneyNum = Math.Max(0, moneyNum - num);
                _playerData.MoneyTypeDataDic[moneyId] = moneyNum;
                GameEntry.Event.Fire(this, ChangeCurrencyEventArgs.Create(moneyId, moneyData.ItemEnumType, moneyNum));
                CurrencyEvent(moneyId, currencyEvent, false, num, moneyNum);
            }
        }

        public void AddCurrency(int[] moneyIds, long[] num, CurrencyEvent currencyEvent)
        {
            for (int i = 0; i < moneyIds.Length; i++)
            {
                if (i >= num.Length)
                {
                    break;
                }
                else AddCurrency(moneyIds[i], num[i], currencyEvent);
            }
        }

        public void AddCurrency(int moneyId, long num, CurrencyEvent currencyEvent)
        {
            var dtMoney = GameEntry.Data.GetData<MoneyEntity>();
            if (dtMoney.TryGetMoneyData(moneyId, out var moneyData))
            {
                var moneyNum = GetCurrency(moneyId, moneyData.InitNum) + num;
                _playerData.MoneyTypeDataDic[moneyId] = moneyNum;
                GameEntry.Event.Fire(this, ChangeCurrencyEventArgs.Create(moneyId, moneyData.ItemEnumType, moneyNum));
                CurrencyEvent(moneyId, currencyEvent, true, num, moneyNum);
            }
        }

        private void CurrencyEvent(int moneyId, CurrencyEvent currencyEvent, bool isAdd, long procNum, long afterNum)
        {
            Dictionary<string, string> dic = new()
            {
                { "U", UPTrace.UPTraceApi.getUserId() },
                { "EVENT", currencyEvent.ToString() },
                { "TYPE_MONEY", moneyId.ToString() },
                { "X_MONEY", procNum.ToString() },
                { "AFTER_MONEY", afterNum.ToString() },
                { "L", "0" },
                { "G", GetCurBattleLevel().ToString() },
                { "LC", "0" },
            };
            string changeType = "0";
            if (isAdd == false)
            {
                changeType = "1";
            }
            dic.Add("X", changeType);
            UPTrace.UPTraceApi.traceDictionary("T0G", dic);
        }
    }
}
